<?php
include("common/header.html");
include("common/left.html");
?>
</head>
<style>
#input_w{width:30px;}
</style>
<body>
<link rel="stylesheet" type="text/css" href="css/wechat_t.css">
<div class="r_content" id="bind_tao">     
<h3 class="mc_title">
    <span class="mctit_1"><span class="mctit_2">微信公众号配置</span></span>
</h3>
<div class="con_content">
    <form action="./call/p.php" id="p03Form" method="post">
        <div class="user_msg" style="width:740px;">
            <ul>
                <div class="wx_kuang_a" style="background-color:#FFFABC">
                    <li style=" padding-left:5%; padding: 10px;font-size: 16px;color:red;">请勿关闭微信公众平台的开发者模式，否则以下配置无法生效&nbsp;&nbsp;&nbsp;<a href="wechat_tutorial.html" target="_blank" class="lnk_help">帮助文档</a></li>
                </div>
                    <li >请输入您的微信公众平台账号密码，进行验证</li>
                     <form>
                        <div class="wx_kuang">
                        <li><b>账号：</b><input type="text" class="txt" name="wechatIds" id="wechatIds" style="width:30%" value="<?=$conf['username']?>"></li>
                        <li><b>密码：</b><input type="password" class="txt" name="wechatSecrets" id="wechatSecrets" style="width:30%" value=""></li>
                        <li class="yan" style="display:none">
                        <b>验证码:</b><input type="text" class="yan_z" name="code" />
                        <img src="" id="imgcode" class="img_a" /><a href="javascript:;" id="changCode">换一张</a>
                        </li>
                        <input type="button" class="submit s_e" id="s_e" value="验证账号"/>
                        </div>
                    </form>
                 <div class="wx_kuang_b">
                <span id="service_span_a" style="display: 'none';">
                <li>将下面信息复制到微信公众账号中（开发者中心&gt;服务器配置）</li>
                    <div class="wx_kuang_f">
                        <li>
                            <b>公众号类型：</b>
                            <label title="服务号">服务号<input type="radio" id="input_w" name="ac_type" value="1" checked="checked"></label>
                            <label title="订阅号">订阅号<input type="radio" id="input_w" name="ac_type" value="0" ></label>
                        </li>
                       <li><b>URL：</b><input type="text" class="highlight" id="url_d" style="width:50%" value=""/></li>
                       <li><b>Token：</b><input type="text" class="highlight" id="token_d" style="width:50%" value="<?=$conf['token']?>"></li>
                       <li><b>encodingAESKey：</b><input type="text" class="highlight" id="encodingAESKey" style="width:52%" value="" /></li>
                    </div>
                </span> 
                </div>
                 <div class="wx_kuang_c">
                 <span id="service_span" style="display: <?=$conf['id']? 'block':'none'?>;">
                 <li>将微信公众账号中信息复制到本网站（开发者中心&gt;开发者ID）</li>
                    <div class="wx_kuang_g">
                        <input type="hidden" name="action" value="update"/>
                        <li><b>微信公众号名称：</b><input type="text" name="Appname" id="Appname" style="width:50%" value="<?=$conf['name']?>"></li>
                        <li><b>AppID：</b><input type="text" name="AppIds" id="AppIds" style="width:50%" value="<?=$conf['appid']?>"></li>
                        <li><b>AppSecret：</b><input type="text" name="AppSecrets" id="AppSecrets" style="width:50%" value="<?=$conf['appsecret']?>"></li>
                    </div>
                </span>
                </div>
            </ul>
            <div class="dz_btn_bottom1" id="wechat_btn" style="display: none;">
                <a href="javascript:void(0)" class="dz_btn1" id="btn_save">确认保存</a>
            </div>
        </div>
    </form>
</div>
</div>
<script type="text/javascript">
    $(document).ready(function($){
        $("#btn_save").on('click', function(){
            var appid       = $("#AppIds").val(),
                Appname     = $("#Appname").val(),
                appsecret   = $("#AppSecrets").val(),
                wechatIds   = $("#wechatIds").val(),
                wechatSecrets  = $("wechatSecrets").val();
            if(appid==''){
                alert('您还尚未填写appid');
                return false;
            }
            if(appsecret==''){
                alert('您还尚未填写AppSecrets');
                return false;
            }
            if(Appname==''){
                alert('您还尚未填写微信公众号名称');
                return false;
            }
            var data = {
                'typeid' : 0,
                'wechatIds' : wechatIds,
                'wechatSecrets' : wechatSecrets,
                'appid' : appid,
                'AppSecrets' : appsecret,
                'Appname' : Appname,
                'action':'update'
            };
            $.ajax({
                url : "module/wechat/bind.php",
                type : "POST",
                dataType : "JSON",
                data : data,
                success : function(res) {
                    if (res.status== 'ok') {
                        alert(res.msg);
                        //                    $("#token_d").text(res.token);
                        //                    $("#url_d").text(res.url);
                        //                    $("#encodingAESKey").text(res.encodingAESKey);
                    } else if (res.status== 'fail') {
                        alert(res.msg);
                    }
                }
            });
        });

        $('.submit').on('click', function(){
            var user = $('input[name=wechatIds]').val(),
                pass = $('input[name=wechatSecrets]').val();
            if(user==''){
                alert('您还尚未填写账号');
                return false;
            }
            if(pass==''){
                alert('您还尚未填写密码');
                return false;
            }
            var data = {
                'user' : user,
                'pass' : pass
            };
            if ( $('.img_a').attr('src') != '' ) {
                var code = $('input[name=code]').val();
                data.code = code;
            }
            $.post('module/wechat/wx_login.php', data, function(res){
                var wechatId = $("#wechatIds");
                var wechatSecret = $("#wechatSecrets");
                //console.log(oRes);
                // -8 需要验证码
                if ( res.status == 'fail' && res.code=='-8') {
                    $('.img_a').attr('src','module/wechat/code.php?u='+user);
                    $('.yan').show();
                    alert('绑定失败');
                } else if( res.status == 'ok' ){
                    wechatId.attr("readOnly",'true');
                    wechatSecret.attr("readOnly",'true');
                    //显示写入的数据
                    $("#service_span_a").show();
                    $("#url_d").val(res.url);
                    $("#token_d").val(res.token);
                    $("#encodingAESKey").val(res.encodingAESKey);

                    $("#service_span").show();
                    $("#wechat_btn").show();
                    $("#s_e").hide();
                    $("#wechatIds").css({backgroundColor:"#e5e5e5"});
                    $("#wechatSecrets").css({backgroundColor:"#e5e5e5"});
                } else {
                    alert(res.msg);
                }
            }, 'json');
            return false;
        });
        $('#changCode').on('click', function(){
            var user = $('input[name=user]').val();
            $('.img_a').attr('src','module/wechat/code.php?u='+user+'&s='+Math.random(1));
        });
    });
</script>
<?include 'common/footer.html';?>